import { Toast } from '../../libs/uniApi.js';

function genDefaultPartitionPrice(startTime, endTime, duratition, price) {
	return {
		price: price,
		retPrice: 0,
		duratition: duratition, 			// timeMode=1时有效
		startTime: startTime, 				// 开始时间，timeMode=2时有效
		endTime: endTime, 					// 结束时间，timeMode=2时有效
		extensionOne: 0,
		extensionTwo: 0,
	}
}
function genDefaultPartition(name, timeMode, seatMode, retPriceType, seatFrom, seatTo, price) {
	let r = {
		id: 0,
		name: name,
		seatCount: undefined,
		seatFrom: undefined,
		seatTo: undefined,
		timeMode: timeMode, 			// 1-fix_time_mode, 2-free_time_mode
		seatMode: seatMode, 			// 1-free_choose_mode, 2-random_lottery_mode
		scoreMode: 0,
		allowChangeSeat: 0,
		changeSeatPrice: 1,
		maxPrepareTime: 15,				// 最大准备时长
		retPriceType: retPriceType, 	// 回鱼计价方式：1-按斤计价, 2-按条计价
		prices: [
			genDefaultPartitionPrice('06:00', '10:00', 4, price)
		],
		seats: []
	};
	
	if(seatFrom) {
		for(let i = seatFrom; i <= seatTo; i++) {
			r.seats.push(genDefaultSeat(i))
		}	
	}
	
	return r;
}
function genDefaultSeat(i) {
	return {
		seatNo: i,		// 钓位编号
		useDevice: 0,	// 是否有设备
		deviceSn: '',	// 设备序列号
	}
}

function MyTip(msg, duration) {
	Toast(msg, 'none', {}, duration || 1800);
}

function genUniqueKey() {
	let random_part = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); // 生成4个十六进制字符，例如:'83bb'（我试了即使紧挨着的连续2次调用生成的都不同）
	return random_part + getTimestampHex();
}

function getTimestamp() {
	return new Date().getTime() // 生成一个number，例如1638877653288
}

function getTimestampHex() {
	return getTimestamp().toString(16) // 当前时间戳转十六进制字符串，例如: '17d94ba2927'
}

// 修正seats配置
function correctSeats(seats, seatFrom, seatTo) {
	// 先删除起止位置外的
	for(let i = 0; i < seats.length; i++) {
		if(seats[i].seatNo < seatFrom || seats[i].seatNo > seatTo) {
			seats.splice(i, 1);
			i--;
		}
	}
	
	// 再补充起止位置内的
	for(let i = seatFrom; i <= seatTo; i++) {
		if(seats.findIndex(a=>a.seatNo===i) === -1)
			seats.push(genDefaultSeat(i));
	}
}

export { genDefaultPartition, genDefaultPartitionPrice, genDefaultSeat, correctSeats, MyTip, genUniqueKey }